<template>
	<!--pages/detail/detail.wxml-->
	<view style='height:100vh;'>
	<scroll-view scroll-y style='height:92vh'>
	  <!-- <swiper>
	        <block wx:for="{{imgUrls}}" wx:key="index">
	          <swiper-item>
	            <image src='{{item}}' style='height:60vh;' />
	          </swiper-item>
	        </block>
	  </swiper> -->
	  <image class="topimage" :src="data.goodsImages.path"/>
	  <view class='title'>
	    {{data.name}}
	    <!-- <view class='spec'>
	    {{data.spec}}
	    </view> -->
	  </view>
	  <view class="title detail">{{data.detail}}</view>
	  <view class="price">
	    <!-- <view class="flex-wrap flex-direction-row" style="margin-top: 18px">
	    	<text class="price-text">￥{{this.$uniApi.accDiv(data.goodsPrice.price, 100)}}/袋</text>
	    	<text class="number"> </text>
	    	  <view class="stepper">
	    		<text :class="[numberBag == 0 ? 'disabled' : 'normal']" @click="bindMinus(0)">-</text>
	    		<input type="number" @input="numberBagInput"  :value="numberBag" />
	    		<text class="normal" @click="bindPlus(0)">+</text>
	    	  </view>
	    </view> -->
	    <view class="flex-wrap flex-direction-row" style="margin-top: 18px">
			<view class="price-text">￥{{data.goodsPrice.price/100}}<span>{{data.goodsPrice.unit}}</span></view>
			<text class="number"> </text>
			  <view class="stepper">
				<text :class="[buyNumber == 0 ? 'disabled' : 'normal']" @click="bindMinus(1)">-</text>
				<input type="number" v-model="buyNumber" />
				<text class="normal" @click="bindPlus(1)">+</text>
			  </view>
	      <!-- <image @click="love" v-if="!isLove"  class="love" src="../../static/love.png"/>
	      <image @click="love" v-else class="love" src='../../static/love-fill.png'/> -->
	    </view>
	    
	  </view>	  
	 
	   <text class="comment desc">产品详情</text>
	   <block v-for="(item,index) in data.goodsImagelist" :key="index">
		   <image class="topimage" :src="item.path"></image>
			<!-- <image @load="imgLoadFunc" :style="'width:100%; height:'+contentImgHeight[index]+'rpx'" :src="item.path"></image> -->
	   </block>
		
	</scroll-view>
	<view class='bottom'>
	  <view class='icon' @click='goShopCar'>
	      <image src='../../static/tab/shopping_cart_y.png' style='width:20px;height:17px;'></image>
	      <text>购物车</text>
	  </view>
	  <view class='btn' @click='addCart'>
	    加入购物车
	  </view>
	  <view class='btn' style='background: #FE4543;' @click='buyNow'>
	    立即购买
	  </view>
	</view>
	
	</view>

</template>

<script>
	var userid = ''
	export default {
		data() {
			return {
				isLove: false,
				olduser: false,
				good: '',
				numberBag: 0,
				buyNumber: 0,
				contentImgHeight: [],
				data:{},
				images: []
			}
		},
		onLoad(options) {
			userid = uni.getStorageSync('userid')
			let that = this
			uni.request({
				url: that.$common.serverAddr +'/goods/info/query',
				data: {id: options.id},
				method: 'POST',
				success: function(res) {
					console.log(res)
					that.data = res.data.data.list[0]
				},
				fail: function() {
					that.$uniApi.tipMsg('获取商品信息失败')
				}
			}) 
		},
		methods: {
			
			bindMinus(flag) {
				if(flag == 0){
					if (this.numberBag > 0) {   
						this.numberBag = this.numberBag - 1
						if(this.numberBag < 0)this.numberBag = 0
					}
				}else{
					if (this.buyNumber > 0) {
						this.buyNumber = this.$uniApi.accSub(this.buyNumber, 1)
						if(this.buyNumber < 0)this.buyNumber = 0
					}
				}
				
			},
			
			bindPlus(flag) {
				if(flag == 0){
					this.numberBag = this.numberBag + 1
				}else{
					this.buyNumber = this.$uniApi.accAdd(this.buyNumber, 1)
					
				}
			},
			
			
			  handleLove: function(flag){
			    
			    let that = this
			    wx.request({
			      url: that.$common.serverAddr+'/OrderSys/HandleLove',
			      data: {
			        flag: flag,
			        userId: that.$common.openID,
			        dishId: that.data.id,
			      },
			      success: function(res) {
			        console.log(res)
			        if(flag == 'exist'){
			          that.isLove = res.data == '1' 
			        }else{
			          that.isLove = !that.isLove 
			          var tip = that.isLove ? '已收藏' : '取消收藏'
			          if(res.data == 'no') tip = '服务器故障，请重试'
			          that.$uniApi.tipMsg(tip)
			        }
			        
			      },
			      fail: function(){
					that.$uniApi.tipMsg('网络故障')
			      }
			    })
			  },
			  love: function(){
			    if(!this.olduser) return
			    var flag = this.isLove ? 'del' : 'add'
			    this.handleLove(flag)
			  },
			  
			  imgLoadFunc: function(e) {
			    var tempHeight = e.detail.height; // 获取图片实际高度
			    this.contentImgHeight.push(tempHeight)
			},
			
			goShopCar() { uni.reLaunch({ url: "/pages/cart/cart" }) },
			  
			addCart(){
			  let data = {
				unit: this.data.goodsPrice.unit,
				customerId: userid,
				productId: this.data.id,
				number: this.buyNumber
			  }
			  this.$uniApi.addItemToCart(data)
			},
			
			  buyNow: function (){
				  
				  if (userid== '') {
				  	this.$uniApi.tellUserLogin()
				  	return
				  }
				  
				  if(this.buyNumber == 0){
					  this.$uniApi.tipMsg("购买数不能是0")
					  return
				  }
			    var arr = []
			    let total = this.$uniApi.accMul(this.buyNumber, this.data.goodsPrice.price)
				this.data.number = this.buyNumber
				this.data.unit = this.data.goodsPrice.unit
			    arr.push(this.data)
			    uni.navigateTo({
			      url: '../cart/order/order?orderlist='+encodeURIComponent(JSON.stringify(arr))
				  +'&totalMoney='+this.$uniApi.accDiv(total, 100)+'&fromCart=0',
			    })
			  },
			
		}
	}
</script>

<style>
@import url("detail.css");
</style>
